package bitbot.md;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class MdManager {
	
	private static MdManager s_instance = new MdManager();
	private final List<PairExchangeData> s_c = new CopyOnWriteArrayList<PairExchangeData>();
	private final DataRequestorThread m_dataThreadOne = new DataRequestorThread(1);
	
	public static MdManager i() {
		return s_instance;
	}
	
	
	void register(PairExchangeData p) {
		s_c.add(p);
	}


	public void unsubscribe(PairExchangeData pairExchangeData, MarketDataType type) {
				
	}

	public void subscribed(PairExchangeData pairExchangeData, MarketDataType type) {
		if(!m_dataThreadOne.isAlive()){
			m_dataThreadOne.start();
		}
	}
	
	private class DataRequestorThread extends Thread {
		private final int m_mdToRequest;
		DataRequestorThread(int mdToReq) {
			super("MD-Requestor");
			m_mdToRequest = mdToReq;
		}
		
		@Override public void run() {
			Exchange ex;
			while(true){
				for (PairExchangeData c : s_c) {
					int mdToReq = c.subscribedMask() | m_mdToRequest;
					if(mdToReq != 0) {
						ex = c.getExchange()
						ex.request(c, mdToReq );
					}
					sleep(10);
				}
			}
			
		}
	}
	  
}
